package com.yj.transport.service;

import com.yj.shipxy.service.ShipxyService;
import com.yj.transport.entity.TransportJournal;
import com.yj.transport.mapper.TransportInfoMapper;
import com.yj.transport.mapper.TransportJournalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengguanyong on 2017/12/6.
 */
@Service
public class TransportJournalService {

    @Autowired
    private TransportJournalMapper transportJournalMapper;

    @Autowired
    private TransportInfoMapper transportInfoMapper;

    @Autowired
    private TransportInfoService transportInfoService;

    @Autowired
    ShipxyService shipxyService;

    @Transactional
    public void createTransportJournal(TransportJournal transportJournal) {
        transportJournalMapper.createTransportJournal(transportJournal);
        if (transportJournal.getPlace_type() == 2) {
            transportInfoMapper.updateTransportStatus(transportJournal.getTransport_info_id());
        }
        TransportJournal lastTransportJournalDetail = transportJournalMapper.getLastTransportJournalDetail(transportJournal.getTransport_info_id());
        transportInfoMapper.modifyCurrentPlace(lastTransportJournalDetail);
    }

    public List<TransportJournal> getTransportJournalListByInfoId(Integer transport_info_id) {
        List<TransportJournal> transportJournalList = new ArrayList<>();
//        TransportInfo transportInfo = transportInfoService.getTransportInfoDetailById(transport_info_id);
//        if (transportInfo.getTransport_type() == 3) {
//            List<ShipTrack> shipTrackList = shipxyService.getShipJournal(transportInfo.getPlate_number());
//            for (ShipTrack shipTrack: shipTrackList) {
//                TransportJournal transportJournal = new TransportJournal();
//                transportJournal.setLongitude(new BigDecimal(shipTrack.getLon()).divide(new BigDecimal(1000000)));
//                transportJournal.setLatitude(new BigDecimal(shipTrack.getLat()).divide(new BigDecimal(1000000)));
//                LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(shipTrack.getUtc(),0, ZoneOffset.ofHours(8));
//                transportJournal.setArrive_time(localDateTime);
//                transportJournalList.add(transportJournal);
//            }
//        } else {
            transportJournalList = transportJournalMapper.getTransportJournalListByInfoId(transport_info_id);
//        }
        return transportJournalList;
    }
}
